<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Services\CourseService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\PracticalTraining;
use App\Services\PracticalTrainingService;

class PracticalTrainingController extends AdminController
{
    //添加
    public function  add(Request $request){
        $validator = Validator::make($request->all(), [
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'pic' => 'required|string',
            'content' => 'required|string',
            'start_time' => 'date',
            'ent_time' => 'date'
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        PracticalTraining::create($request->all());
        return $this->success();
    }
    //修改
    public function  update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'pic' => 'required|string',
            'content' => 'required|string',
            'start_time' => 'date',
            'ent_time' => 'date'
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method','_token','course_name']);
        $practicalTraining = new PracticalTraining();
        $practicalTraining->find($request->id)->update($putData);
        return $this->success();
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'course_id' => 'integer|digits_between:1,20|min:1',
            'name' => 'string',
            'start_time' => 'date',
            'ent_time' => 'date',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $course_id  = $request->input('course_id');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');

        $list = PracticalTrainingService::getInstance()->list($page,$limit,$sort,$order, $name,$course_id);
        return $this->success($list);
    }
    //列表
    public function  userPracticalList(Request $request){
        $validator = Validator::make($request->all(), [
            'course_id' => 'integer|digits_between:1,20|min:1',
            'users_id' => 'integer|digits_between:1,20|min:1',
            'name' => 'string',
            'start_time' => 'date',
            'ent_time' => 'date',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $course_id  = $request->input('course_id');
        $users_id  = $request->input('users_id');

        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');

        $list = PracticalTrainingService::getInstance()->userPracticalList($page,$limit,$sort,$order, $name,$course_id,$users_id);
        return $this->success($list);
    }
    public function  getByPracticalTrainingInfo(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $id = $request->input('id');
        $list = PracticalTrainingService::getInstance()->getByPracticalTrainingInfo($id);
        return $this->success($list);
    }

    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $practicalTraining = PracticalTraining::find($request->id);
        $practicalTraining->delete();
        return $this->success();
    }
    // 上传头像
    public function upfile(Request $request) {
        $url = config('filesystems.disks.practical.url');
        $default = config('filesystems.disks.practical.default');
        $pic =  PracticalTrainingService::getInstance()->upfile($request,$url,$default,'practical');
        return $this->success([
                'pic'=>$pic,
            ]
        );
    }
}
